﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace OrdersManage
{
    [Serializable]
    public class OrderService
    {
        public OrderService() { }
        public List<Order> orders = new List<Order>();
        public void showOrders()
        {
            foreach (Order a in this.orders)
            {
                Console.WriteLine(a);
                a.showDetails();
            }
        }
        public void addOrder(Order a)
        {
            try
            {
                foreach (Order o in orders)
                {
                    if (o.Equals(a))
                    {
                        Exception e1 = new Exception("订单重复！添加失败");
                        throw e1;
                        //Console.WriteLine("订单重复！添加失败");
                        //return;
                    }
                }
                orders.Add(a);
            }
            catch (Exception exception)
            {
                Console.WriteLine("订单重复！添加失败");
            }
        }
        public void removeOrder(int num)
        {
            try
            {
                int count = orders.Count;
                int index = 0;
                foreach (Order o in orders )
                {
                    if(o.OdNum == num)
                    {
                        orders.RemoveAt(index);
                    }
                    index++;count--;
                    if(count == 0)
                    {
                        Exception e2 = new Exception("该订单不存在！");
                            throw e2;
                    }
                }
            }
            catch { Console.WriteLine("该订单不存在！"); }
        }
        public void Sort() 
        { 
            orders.Sort();
        }
        public Order SearchOrder(int num)
        {
            try
            {
                var q = from o in orders
                        where o.OdNum == num
                        select o;
                return q.FirstOrDefault();
            }
            catch (Exception e)
            {
                Console.WriteLine("该订单号无对应订单！");
                throw e;
            }
        }
        public Order SearchOrder(string client)
        {
            try
            {
                var q = from o in orders
                        where o.Client == client
                        select o;
                return q.FirstOrDefault();
            }
            catch (Exception e)
            {
                Console.WriteLine("该客户名无对应订单！");
                throw e;
            }
        }
        public Order SearchOrder(double money)
        {
            try
            {
                var q = from o in orders
                        where o.money == money
                        select o;
                return q.FirstOrDefault();
            }
            catch (Exception e)
            {
                Console.WriteLine("未找到对应金额订单！");
                throw e;
            }
        }
        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("Orders.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fs, orders);
            }
        }
        public void Import()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("Orders.xml", FileMode.Open))
            {
                orders = (List<Order>)xmlSerializer.Deserialize(fs);
            }
        }


    }
}
